
** cross validation exercise using jackknife/leave one out procedure

***Inputs: 
* $Data/GameUserWPdaily_norm.dta

***Outputs: 
* $Data/leaveonedirectionsmonth_zip260.dta
* $Results/jackknifesample_highpm
* $Results/splitsamples.tex

		
	use $Data/GameUserWPdaily_norm.dta, clear

	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 

	gen monthyear = mofd(date)

	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360	
	
	gen month = month(date)
	
	
	
	** for each zip2, leave out one zip at a time, find IV for remainder 
	
	gen zip2 = int( zip/10)
	
	levelsof zip2
	local zip2 = r(levels)
	

	foreach zip in `zip2' {
	
	levelsof zip if zip2==`zip'
	local leave = r(levels)
	
	
	foreach leftout in `leave' {
		
		
	quietly	capture reghdfe highpm  i.cut_wind#i.month  if zip2 ==`zip'  & zip~=`leftout',   absorb( anon_id  monthyear) cluster(anon_id monthyear )
				
		capture regsave using $Results/leaveonewind`zip'_`leftout', replace 
		
				
	}
	
	}
	
	
	clear
	local files : dir "$Results/" files "leaveonewind*.dta"
	
	gen file =""
	
foreach file in `files'{
		append using  $Results/`file'
		*capture erase  $Results/leaveonewind`zip'_`leftout'
		replace file=  "`file'" if file==""
}

gen zip= substr(file,-7,3)

destring zip, replace ignore("_")

	
	
	
	gen direction = substr(var,1,3)
	destring direction, replace force ignore("c" "."  "u" "ons" "b")

	gen month = substr(var,-9,3)
	destring month, replace force ignore("c" "."  "u" "o" "b" "#" "n")

	
	replace coef = . if r2==. // 
	
	by zip month, sort: egen maxcoef= max(coef) if direction<.  & coef <. 


	gen maxdirection = direction if maxcoef ==coef & maxcoef >=0  & maxcoef<. 
	

	
	collapse (firstnm) maxdirection , by(zip month)
	
	drop if month ==. 
		
	save $Data/leaveonedirectionsmonth_zip260.dta, replace
	
	

	
	use $Data/GameUserWPdaily_norm.dta, clear
	
	gen month = month(date)

	
	egen cut_winddirection = cut(median_closestwind), at(0(60)360)
	replace cut_winddirection = 0 if median_closestwind ==360
		
	
	merge m:1 zip month using $Data/leaveonedirectionsmonth_zip260.dta, keepusing(maxdirection)
	
	
	
	gen monthyear = mofd(date)

	gen highpm =  weighted_dailypm > 25 & weighted_dailypm<. 

	
	gen maxzipwind = (cut_winddirection ==maxdirection )
	replace maxzipwind = . if maxdirection==.
	
	keep if maxzipwind<. 
	

	
	egen cut_temp = cut(weighted_temperature), at(-40(10)40)
		
		
		
		
		
ivreghdfe norm_score (highpm =maxzipwind )  weighted_wind_speed weighted_precipitation , absorb(anon_id monthyear cut_temp) cluster(anon_id monthyear)

eststo jackknife 


		estimates save $Results/jackknifesample_highpm , replace

		
		********* export results
		
		eststo clear 
		
		estimates use $Results/jackknifesample_highpm 
		eststo jackknife

		
		
		estimates use  $Results/splitsample_highpm
		eststo split
		
		
		estimates use  $Results/timesplitsample_highpm
		eststo splittime 
		
		
		
		esttab split splittime jackknife using $Results/splitsamples.tex, drop(*weighted_wind_speed *weighted_precipitation) replace se scalars("widstat First stage F") nocon star(* 0.1 ** 0.05 *** 0.001) label nomtitles
	
